iPhoneJS.extend({
	get: function( url, data, callback, type ) {
		return iPhoneJS.ajax({
			type: "GET",
			url: url,
			data: data,
			success: callback,
			dataType: type
		});
	},
	
	ajaxSettings: {
		url: location.href,
		type: "GET",
		async: true,
		xhr:function(){
			return window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
		}
	},
	
	ajax : function(s) {
		s = iPhoneJS.extend(true, s, iPhoneJS.extend(true, {}, iPhoneJS.ajaxSettings, s));
		var status, data, type = s.type.toUpperCase(), requestDone = false;;

		// Create the request object
		var xhr = s.xhr();
		
		xhr.open(type, s.url, s.async);
			
		// Wait for a response to come back
		var onreadystatechange = function(){
			if( xhr && xhr.readyState == 4 ){
				status = !iPhoneJS.httpSuccess( xhr ) ? "error" : "success";
	
				if ( status == "success" ) {
					// Watch for, and catch, XML document parse errors
					try {
						// process the data (runs the xml through httpData regardless of callback)
						data = iPhoneJS.httpData( xhr, s.dataType, s );
					} catch(e) {
						status = "parsererror";
					}
					success();
				}
				else
					iPhoneJS.handleError(s, xhr, status);
	
				// Fire the complete handlers
				complete();
				
				xhr = null;
			}
		};
		
		// Send the data
		try {
			xhr.onreadystatechange = onreadystatechange;
			xhr.send(s.data);
		} catch(e) {
			iPhoneJS.handleError(s, xhr, null, e);
		}
		
		function success(){
			// If a local callback was specified, fire it and pass it the data
			if ( s.success ) s.success( data, status );
		}

		function complete(){
			// Process result
			if ( s.complete ) s.complete(xhr, status);
		}

		// return XMLHttpRequest to allow aborting the request etc.
		return xhr;
	},
		
	handleError: function( s, xhr, status, e ) {
		// If a local callback was specified, fire it
		if ( s.error ) s.error( xhr, status, e );
	},
	
	// Determines if an XMLHttpRequest was successful or not
	httpSuccess: function( xhr ) {
		try {
			// IE error sometimes returns 1223 when it should be 204 so treat it as success, see #1450
			return !xhr.status && location.protocol == "file:" ||
				( xhr.status >= 200 && xhr.status < 300 ) || xhr.status == 304 || xhr.status == 1223;
		} catch(e){}
		return false;
	},
	
	httpData: function( xhr, type, s ) {
		var ct = xhr.getResponseHeader("content-type"),
			xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0,
			data = xml ? xhr.responseXML : xhr.responseText;

		if ( xml && data.documentElement.tagName == "parsererror" )
			throw "parsererror";

		return data;
	}
	 	
});